﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using Portal.Common.Entity;
using Portal.Logic;
using Portal.Common.Interface;
using Common.Service;
using OC.Common.Entity;
using Common.BasePages;

namespace Portal
{
    public partial class SettingPage : BasePage
    {
        public string TabIndex;
        public string strPath;
        private UserEntity _User;
        public UserEntity User 
        {
            get 
            {
                if (_User == null)
                {
                    _User = UserInfo as UserEntity;
                }
                return _User;
            }
        }

        private IPersonSetting iPersonSetting;
        public IPersonSetting IPersonSettingService 
        { 
            get 
            {
                if (iPersonSetting == null)
                {
                    iPersonSetting = ServiceContainer.GetService<IPersonSetting>();
                }
                return iPersonSetting;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                TabIndex = "0";
                BindData(0);
            }
        }
       

        public void BindData(int index) 
        {
            
            #region 绑定提示设置
            List<PersonSettingEntity> item1 = SettingCountByUser(User.USER_ID, "1");
            if (item1 != null && item1.Count > 0)
            {
                foreach (PersonSettingEntity entity in item1)
                {
                    string[] str = entity.SETTING_VALUE.Split(',');
                    for (int i = 0; i < str.Length; i++)
                    {
                        switch (str[i])
                        {
                            case "1":
                                cboEmail.Checked = true;
                                break;
                            case "2":
                                cboMobile.Checked = true;
                                break;
                            case "3":
                                cboIM.Checked = true;
                                break;
                            case "4":
                                cboMsg.Checked = true;
                                break;
                            default:
                                break;
                        }
                    }
                }
            }
            #endregion
            #region 绑定常用语设置
            List<PersonSettingEntity> item2 = SettingCountByUser(User.USER_ID, "2");
            if (item2 != null && item2.Count > 0)
            {
                PagedDataSource page = new PagedDataSource();
                int pageSize = 5;
                int totalCount = item2.Count;
                page.DataSource = item2;
                page.AllowPaging = true;
                page.PageSize = pageSize;
                page.CurrentPageIndex = index;

                rptList.DataSource = page;
                rptList.DataBind();

                pagination.RecordCount = totalCount;
                pagination.PageSize = page.PageSize;
                pagination.CurrentPageIndex = page.CurrentPageIndex + 1;
                pagination.Visible = true;
                pagination.AlwaysShow = true;
                pagination.CurrentPageButtonClass = "btnPageNavCurrentInput";
                pagination.DataBind();
            }
            #endregion
        }
        /// <summary>
        /// 根据用户ID查询用户个人设置条数
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public List<PersonSettingEntity> SettingCountByUser(string userId, string itemsType) 
        {
            string count = string.Empty;
            List<PersonSettingEntity> lists = new PersonSettingLogic().GetListByUserId(userId);
            List<PersonSettingEntity> items= new List<PersonSettingEntity>();
           
            if (lists != null)
            {
                if (lists.Count > 0)
                {

                    items = lists.FindAll(delegate(PersonSettingEntity ety)
                    {
                        if (ety.SETTING_TYPE == itemsType)
                        {
                            return true;
                        }
                        return false;
                    });

                }
            }
            return items;
            
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            TabIndex = "0";
            string value = string.Empty;
            
            PersonSettingEntity entity = new PersonSettingEntity();
            entity.CREATE_BY = User.USER_CODE;
            entity.CREATE_DATE = DateTime.Now;
            entity.DELEGATOR_STATUS = "0";
            entity.IS_ACTIVE = "1";
            entity.SETTING_ITEMS = "通知";
            if (cboEmail.Checked) 
            {
                value += "1"+",";
            }
            if (cboMobile.Checked)
            {
                value += "2" + ",";
            }
            if (cboIM.Checked)
            {
                value += "3" + ",";
            }
            if (cboMsg.Checked)
            {
                value += "4" + ",";
            }
            entity.SETTING_VALUE = value.Substring(0,value.Length-1);
            entity.SETTING_TYPE = "1";
            entity.SETTING_SORT = 1;
            entity.UPDATE_BY = User.USER_CODE;
            entity.UPDATE_DATE = DateTime.Now;
            entity.USER_ID = User.USER_ID;
            entity.EXT01 = "";
            IPersonSettingService.InserData(entity);
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            TabIndex = "1";
            string strId = string.Empty;
            foreach (RepeaterItem item in rptList.Items)
            {
                HtmlInputCheckBox cbo = ((HtmlInputCheckBox)item.FindControl("cboList"));
                if (cbo.Checked)
                {
                    strId += ((HiddenField)item.FindControl("hidListID")).Value + ",";
                }
            }
            strId = strId.Substring(0, strId.Length - 1);
            int count = IPersonSettingService.DeleteById(strId);
        }

        protected void btnSaveSetting_Click(object sender, EventArgs e)
        {
            TabIndex = "2";
            
            PersonSettingEntity entity = new PersonSettingEntity();
            entity.CREATE_BY = User.USER_CODE;
            entity.CREATE_DATE = DateTime.Now;
            entity.DELEGATOR_STATUS = "0";
            entity.EFFECT_FROM = DateTime.Now;
            entity.IS_ACTIVE = "1";
            entity.SETTING_ITEMS = "个人签名";
            entity.SETTING_VALUE = strPath;
            entity.SETTING_TYPE = "3";
            entity.SETTING_SORT = 1;
            entity.UPDATE_BY = User.USER_CODE;
            entity.UPDATE_DATE = DateTime.Now;
            entity.USER_ID = User.USER_ID;
            IPersonSettingService.InserData(entity);
        }

        protected void btnUpLoad_Click(object sender, EventArgs e)
        {
            TabIndex = "2";
            string savePath = @"C:\UpLoadImg\" + DateTime.Now.ToString("yyyyMMddhhmmss");
            string imgName = fileUpLoad.FileName;
            if (!Directory.Exists(savePath))
            {
                Directory.CreateDirectory(savePath);
                string fileName = System.IO.Path.GetFileName(imgName);
                if (fileName == "")
                {
                    Page.RegisterClientScriptBlock("fail", "<script>alert('请选择要上传的图片！');</script>");
                    return;
                }
                else
                {
                    if (fileName.Substring(fileName.LastIndexOf('.')) == ".jpg" || fileName.Substring(fileName.LastIndexOf('.')) == ".jpeg" || fileName.Substring(fileName.LastIndexOf('.')) == ".gif")
                    {
                        strPath = Path.Combine(savePath, fileName);
                        fileUpLoad.PostedFile.SaveAs(Path.Combine(savePath, fileName));
                        Page.RegisterClientScriptBlock("ok", "<script>alert('图片上传成功！');</script>");

                    }
                    else
                    {
                        Page.RegisterClientScriptBlock("fail", "<script>alert('所选文件不符合格式，请重新选择！');</script>");
                        return;
                    }
                }
            }
            else
            {
                string fileName = System.IO.Path.GetFileName(imgName);
                if (fileName == "")
                {
                    Page.RegisterClientScriptBlock("fail", "<script>alert('请选择要上传的图片！');</script>");
                    return;
                }
                else
                {
                    if (fileName.Substring(fileName.LastIndexOf('.')) == ".jpg" || fileName.Substring(fileName.LastIndexOf('.')) == ".jpeg" || fileName.Substring(fileName.LastIndexOf('.')) == ".gif")
                    {
                        strPath = Path.Combine(savePath, fileName);
                        fileUpLoad.PostedFile.SaveAs(Path.Combine(savePath, fileName));
                        Page.RegisterClientScriptBlock("ok", "<script>alert('图片上传成功！');</script>");
                    }
                    else
                    {
                        Page.RegisterClientScriptBlock("fail", "<script>alert('所选文件不符合格式，请重新选择！');</script>");
                        return;
                    }
                }
            }
        }
        /// <summary>
        /// 转换成相对路径
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public string GetMapPath(string path)
        {
            string tmpRootDir = Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath.ToString());//获取程序根目录
            string imagesurl2 = path.Replace(tmpRootDir, ""); //转换成相对路径
            imagesurl2 = imagesurl2.Replace(@"\", @"/");
            //imagesurl2 = imagesurl2.Replace(@"Aspx_Uc/", @"");
            return imagesurl2;

        }
        /// <summary>
        /// 转换成绝对路径
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        public string GetPath(string path)
        {
            string tmpRootDir = Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath.ToString());//获取程序根目录 
            string imagesurl2 = tmpRootDir + path.Replace(@"/", @"\"); //转换成绝对路径 
            return imagesurl2;

        }

       


    }
}
